import numpy as np

array = np.array([[1, 2, 3], [2, 3, 4]], dtype=np.int64)
# dtype -> 指明矩阵存储的数据类型

array_zero = np.zeros((2, 3))
array_ones = np.ones((2, 3))

print(array)
print(f"维度：{array.ndim}")
print(f"shape:{array.shape}")
print("size:", array.size)
print("type:", array.dtype)

print(array_zero)
print(array_ones)

print("-" * 10)

a = np.arange(1, 10, 3)  # arange(1, 10) => 1 ~ 9
print(a)

b = np.arange(12).reshape(3, 4)
print(b)

c = np.linspace(1, 10, 6).reshape((2, 3))
print(c)

print("-" * 10)

d = np.array([[1, 2], [3, 4]])
e = np.arange(4).reshape((2, 2))
f = d + e
print(f"d: \n{d}")
print(f"e: \n{e}")

print("-" * 10)

print(f)
print(f < 3)  # [ True False False False]

print("-" * 10)

# 矩阵乘法
g = np.dot(d, e)
# g = d.dot(e)  # 结果相同
print(g)

print("-" * 10)

# random.random()随机产生一个 0 ~ 1 的数字
h = np.random.random(size=(2, 4))  # shape=(2, 4)
print(f"h: \n{h}")
print(f"sum:{np.sum(h, axis=0)}")  # axis=0 按列     axis=1 按行
print(f"max:{np.max(h, axis=0)}")
print(f"min:{np.min(h, axis=0)}")

print("argmin: ", np.argmin(h))  # 矩阵索引也是从 0 开始  shape(3, 4) index => 0 ~ 11
print("argmax: ", np.argmax(h))

print(np.mean(h))  # 平均值
print(np.median(h))  # 中位数

print("-" * 10)

i = np.array([[0, 1, 2, 3], [4, 5, 7, 7]])
print(np.cumsum(i))  # 输出每一项从index=0 ~ index=当前 的累加的结果
print(np.diff(i))  # 每一项与后一项的差值

# (array([0, 0, 0, 1, 1, 1, 1], dtype=int64), array([1, 2, 3, 0, 1, 2, 3], dtype=int64))
print(np.nonzero(i))  # 上述结果，第一个array的数字与第二个array对应数字构成非零元素的行列坐标

print(np.transpose(i))  # 矩阵的转置
print(i.T)

print(np.clip(i, 2, 5))  # 让所有小于2的数变成2，大于5的变成5

for column in i.T:  # 逐行输出矩阵
    print(column)

print("-" * 10)

# 矩阵的合并
# A = np.array([1, 1, 1])[np.newaxis, :]
# B = np.array([2, 2, 2])[np.newaxis, :]
# print("A:", A, f"A.shape:{A.shape}")
# print("B:", B, f"B.shape:{B.shape}")
#
# C = np.vstack((A, B))   # vertical stack
# D = np.hstack((A, B))   # horizontal stack
# print(C)
# print(D)
#
# D = np.concatenate((A, B), axis=1)
# print("D:", D)

# print(A[np.newaxis, :])
# A = A[np.newaxis, :]
# print(A.T)
# print(A.T[:, np.newaxis])

# 矩阵的分割
A = np.arange(12).reshape((3, 4))
print(A)

print("array_split:", np.array_split(A, [2, 3], axis=1))  # array_split 不均匀分割
print(np.array_split(A, 3, axis=1))
print("split:", np.split(A, 3, axis=0))   # 分割
print("vsplit:", np.vsplit(A, 3))
print("hsplit:", np.hsplit(A, 4))


print("-" * 10)

arr = ([11, 1, 2, 4])
a1 = arr
b1 = a1
c1 = b1

b1[1:3] = [22, 33]
print(arr)  # [11, 22, 33, 4] 类似引用
print(a1)    # [11, 22, 33, 4]
print(b1)    # [11, 22, 33, 4]
print(c1)    # [11, 22, 33, 4]

e1 = arr.copy()   # deep copy  重新建立内存空间存储
e1[1:3] = [21, 23]
print(arr)
print(e1)
